import { getLocalesCommonOssUrl } from '@src/util/assets';
import { t } from '@src/locales';
import { cloneDeep } from 'lodash';
function initIconList(total = 20, pageSize = 8) {
  const res_ = [];
  let page = [];
  for (let index = 0; index < total; index++) {
    page.push(getLocalesCommonOssUrl(`/faultLibrary/${index}.png`));
    if (page.length === pageSize || index === total - 1) {
      res_.push(page);
      page = [];
    }
  }
  return res_;
}

// 修改需要同步修改移动端同文件
export const iconList = initIconList();
export function getListItem(e: any) {
  let arr = [];
  if (e) {
    arr = e.split('-');
  }
  const index = arr[0] || 0;
  const index2 = arr[1] || 0;
  return iconList[index][index2];
}

export const validateFaultDesc = (rule, value, callback) => {
  if (!value) {
    callback(new Error(t('common.base.tip.defaultPlaceholder')));
  } else if (value.includes('/')) {
    callback(new Error(t('common.placeholder.notInputSlash')));
  } else {
    callback();
  }
};

export function formatFaultToHttp(data: any) {
  const data_ = cloneDeep(data);
  const { faultDesc, faultLabel, parentId, faultLevel, attachment, faultContent } = data_;
  const params = {
    parentId,
    faultDesc,
    faultLabel,
    faultLevel: 0,
    enterState: 0, // 目前移动端新建只有工单里有入口
    faultContent,
    attribute: {
      attachment,
    },
  };
  return params;
}

export function formatFaultToLocale(data: any) {
  const data_ = cloneDeep(data);

  const attribute = data_?.attribute || {};
  return {
    ...data_,
    ...attribute,
  };
}
